package it.uniroma2.structure;

public class CopyLiteral extends Copy{
	private Literal from;
	private Variable toVar;
	private Parameter toPar;
	private final String queryTag = "<bpel:query queryLanguage=\"urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0\"><![CDATA[";
	private final String queryTagClose = "]]></bpel:query>";
	
	public CopyLiteral(Literal l, Variable var){
		this.from = l;
		this.toVar = var;
		this.toPar = null;
	}
	
	public CopyLiteral(Literal l, Parameter p){
		this.from = l;
		this.toPar = p;
		this.toVar = null;
	}
	
	@Override
	public String toBPEL() {
		String output = "";
		output = output + "<" + copyTag + ">\n";
		output = output + "<" + fromTag + ">\n";
		output = output + this.from.toBPEL();
		output = output + "</" + fromTag + ">";
		if(this.toVar != null){
			if(this.toVar instanceof VariableProcess){
				output = output + "<" + toTag + " variable=\"" + this.toVar.getName() + "\" part=\"" + ((VariableProcess)this.toVar).getPart() + "\">\n";
			}else if(this.toVar instanceof VariableService){
				output = output  + "<" + toTag + " variable=\"" + this.toVar.getName() + "\" part=\"" + ((VariableService)this.toVar).getPart() + "\">\n";
			}
		}
		else{
			if(this.toPar.getVariable() instanceof VariableService){
				
				output = output + "<" + toTag + " variable=\"" + this.toPar.getVariable().getName() + "\" part=\"" + ((VariableService)this.toPar.getVariable()).getPart() + "\">\n";
				output = output + this.queryTag + ((VariableService)this.toPar.getVariable()).getNamespace() + ":" + this.toPar.getName() + this.queryTagClose + "\n";
			}else{
				output = output + "<" + toTag + " variable=\"" + this.toPar.getVariable().getName() + "\" part=\"" + ((VariableProcess)this.toPar.getVariable()).getPart() + "\">\n";
				output = output + this.queryTag + ((VariableProcess)this.toPar.getVariable()).getPrefix() + ":" + this.toPar.getName() + this.queryTagClose + "\n";
			}
		}
		output = output + "</" + toTag + ">";
		output = output + "</" + copyTag + ">";
		
		return output;
	}

	public Parameter getToPar() {
		return toPar;
	}

	public void setToPar(Parameter toPar) {
		this.toPar = toPar;
	}
}
